<?php
function http_response($url,$proxy="http://10.10.10.78:8080",$usename="zhourh2",$password="840802xY")
{
    //return file_get_contents($url);
    // 1. 初始化
    $ch = curl_init();
    // 2. 设置选项，包括URL
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt ( $ch, CURLOPT_PROXY, $proxy);//代理网址和端口
    curl_setopt ( $ch, CURLOPT_PROXYUSERPWD, $usename.":".$password);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    // 3. 执行并获取HTML文档内容
    $output = curl_exec($ch);

    if ($output === FALSE)
    {
        echo "cURL:".$url." Error: " . curl_error($ch);
    }
    // 4. 释放curl句柄
    curl_close($ch);
    return $output;
}

function get_max_page($html)
{
    $pattern = '|<li class="page_no"><a href="/shop_list/(.+?).html">([\d]+)</a></li><li class="page_next_yes">|';
    $result = preg_match($pattern,$html,$matchs);
    if ($result === 0)
        return 0;
    return $matchs[2];
}

function expansion_to_sub_page($first_page_url,$page_total_num)
{
    if ($page_total_num === 0)
    {
        $page_list[] = $first_page_url;
        return $page_list;
    }

    $find = '|^/shop_list/i_1|';
    for ($page_index=1;$page_index<=$page_total_num;$page_index++)
    {
        $page_list[] = preg_replace($find,'/shop_list/i_'.$page_index, $first_page_url);
    }
    return $page_list;
}

function get_file_list($dir_path,$max_count = 0)
{
    $i =0;
    $current_dir = opendir($dir_path);    //opendir()返回一个目录句柄,失败返回false
    while (($file = readdir($current_dir)) !== false)     //readdir()返回打开目录句柄中的一个条目
    {
        $sub_dir = $dir_path . DIRECTORY_SEPARATOR . $file;    //构建子目录路径
        if ($file == '.' || $file == '..' || is_dir($sub_dir))
        {
            continue;
        }
        else      //如果是文件,直接输出
        {
            $file_list[] = $file;
            if($max_count && $i++ > $max_count)
            break;
        }
    }
    return $file_list;
}

?>